package com.ruoyi.aiexperiments.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.aiexperiments.domain.ExperimentTraining;
import com.ruoyi.aiexperiments.service.IExperimentTrainingService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 实验训练记录Controller
 * 
 * @author czj
 * @date 2025-01-06
 */
@RestController
@RequestMapping("/training")
public class ExperimentTrainingController extends BaseController
{
    @Autowired
    private IExperimentTrainingService experimentTrainingService;

    /**
     * 查询实验训练记录列表
     */
    @RequiresPermissions("aiexperiments:training:list")
    @GetMapping("/list")
    public TableDataInfo list(ExperimentTraining experimentTraining)
    {
        experimentTraining.setDeleteFlag(0);
        startPage();
        List<ExperimentTraining> list = experimentTrainingService.selectExperimentTrainingList(experimentTraining);
        return getDataTable(list);
    }

    /**
     * 导出实验训练记录列表
     */
    @RequiresPermissions("aiexperiments:training:export")
    @Log(title = "实验训练记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExperimentTraining experimentTraining)
    {
        List<ExperimentTraining> list = experimentTrainingService.selectExperimentTrainingList(experimentTraining);
        ExcelUtil<ExperimentTraining> util = new ExcelUtil<ExperimentTraining>(ExperimentTraining.class);
        util.exportExcel(response, list, "实验训练记录数据");
    }

    /**
     * 获取实验训练记录详细信息
     */
    @RequiresPermissions("aiexperiments:training:query")
    @GetMapping(value = "/{trainingId}")
    public AjaxResult getInfo(@PathVariable("trainingId") Long trainingId)
    {
        return success(experimentTrainingService.selectExperimentTrainingByTrainingId(trainingId));
    }

    /**
     * 新增实验训练记录
     */
    @RequiresPermissions("aiexperiments:training:add")
    @Log(title = "实验训练记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExperimentTraining experimentTraining)
    {
        experimentTraining.setCreatedBy(SecurityUtils.getUsername());
        experimentTraining.setCreatedAt(new Date());
        return toAjax(experimentTrainingService.insertExperimentTraining(experimentTraining));
    }

    /**
     * 修改实验训练记录
     */
    @RequiresPermissions("aiexperiments:training:edit")
    @Log(title = "实验训练记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExperimentTraining experimentTraining)
    {
        experimentTraining.setUpdatedBy(SecurityUtils.getUsername());
        experimentTraining.setUpdatedAt(new Date());
        return toAjax(experimentTrainingService.updateExperimentTraining(experimentTraining));
    }

    /**
     * 删除实验训练记录
     */
    @RequiresPermissions("aiexperiments:training:remove")
    @Log(title = "实验训练记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{trainingIds}")
    public AjaxResult remove(@PathVariable Long[] trainingIds)
    {
        for (Long trainingId : trainingIds) {
            ExperimentTraining experimentTraining = experimentTrainingService.selectExperimentTrainingByTrainingId(trainingId);
            if (experimentTraining == null) {
                return error("实验训练记录不存在");
            }

            experimentTraining.setDeletedBy(SecurityUtils.getUsername());
            experimentTraining.setDeletedAt(new Date());
            experimentTraining.setDeleteFlag(1);
            experimentTrainingService.updateExperimentTraining(experimentTraining);
        }
        return toAjax(true);
    }
}
